/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.main;

import java.util.Date;

import com.aeky.client.common.DateUtils;
import com.aeky.client.common.DemoConstants;
import com.aeky.client.common.Registry;
import com.aeky.client.common.WidgetUtils;
import com.aeky.client.view.base.ContentApplication;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * 
 * @author aeky
 */
public class MainApplication extends ContentApplication {

    private VLayout mainLayout;

    private HLayout headerLayout;

    private HLayout mainMenu;

    private HLayout bottomLayout;

    public MainApplication() {
        super();
        this.initHeader();
        this.initMenu();
        this.initBottom();
        mainLayout = new VLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();
        mainLayout.addMember(headerLayout, 0);
        mainLayout.addMember(mainMenu, 1);
        mainLayout.addMember(getContentPanel(), 2);
        mainLayout.addMember(bottomLayout, 3);
        Registry.registry(DemoConstants.MAIN_MENU, mainMenu);
        this.addMember(mainLayout);
        this.setHeight100();
        this.setWidth100();
    }

    private void initMenu() {
        mainMenu = new HLayout();
        mainMenu.setHeight(30);
        mainMenu.setWidth100();
    }

    private void initHeader() {
        headerLayout = new HLayout();
        headerLayout.setStyleName("headerLayout");
        headerLayout.setHeight(30);
        headerLayout.setWidth100();

        HLayout divHeaderRight = new HLayout();
        divHeaderRight.setAlign(Alignment.RIGHT);
        divHeaderRight.addMember(WidgetUtils.createPreferenceIconButton());
        divHeaderRight.addMember(WidgetUtils.createOpenHelpDocumentsIconButton());
        divHeaderRight.addMember(WidgetUtils.createLogoutIconButton());
        headerLayout.addMember(divHeaderRight);
    }

    private void initBottom() {
        bottomLayout = new HLayout();
        bottomLayout.setHeight(25);
        bottomLayout.setWidth100();
        final Label timeLabel = new Label();
        timeLabel.setWidth("10%");
        timeLabel.setAlign(Alignment.LEFT);
        new Timer() {
            public void run() {
                timeLabel.setContents(DateUtils.formatDateTime(new Date(),
                        DemoConstants.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS));
                schedule(1000);
            }
        }.schedule(1000);
        bottomLayout.addMember(bottomDiv());
        bottomLayout.addMember(timeLabel);
    }

    private HTML bottomDiv() {
        HTML footer = new HTML("<img src='images/preference.gif' style='vertical-align: middle;padding-bottom:5px'/> "
                + "Copyright © 2013 - 2013 Aeky. All Rights Reserved");
        footer.setWidth("90%");
        footer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        return footer;
    }
}
